package testPackage;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;

import java.util.concurrent.TimeUnit;

/**
 * step by step 13 自动滚动网页
 * javascript的scrollBy方法，两个参数分别以像素为单位进行水平和垂直滚动
 */
public class Selenium14 {
    public static void main(String[] args) {

        testScroll();
    }
    //自动滚动的方法
    public static void testScroll(){
        String str = "D:\\\\Enviroments\\\\geckodriver-v0.26.0-win64\\\\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", str);
        WebDriver driver = new ChromeDriver();
        //打开网页
        driver.get("https://www.flipkart.com");
        driver.manage().window().maximize();
        //搜索框中输入搜索信息
        WebElement searchBox = driver.findElement(By.id("fk-top-search-box"));
        searchBox.sendKeys("Buddha");
        //点击搜索按钮
        WebElement searchButton = driver.findElement(By.className("search-bar-submit"));
        searchButton.click();
        //等待3s
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //向下滚动页面
        JavascriptExecutor js = (JavascriptExecutor)driver;
        js.executeScript("scrollBy(0,2500)");
        //等待直到第二页可见
        WebElement pageNumberdisplayer = (new WebDriverWait(driver,10))
                .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.row")));
        //验证第二页可见，并让更多的结果可见
        Assert.assertEquals(pageNumberdisplayer.getText(),"Page:2");
    }
}
